import childrenHeightData from './children_height_data.js'
import DateUtil from '../utils/DateUtils.js'
import MeasureItem from './MeasureItem'
const getDateAge = DateUtil.getDateAge
const Calc = require('./Calc')
const UI = require('./UI')

const STAND_LEVEL = 1

const BAR = [UI.color.report_lower, UI.color.report_stand, UI.color.report_higher]

export default class HeightReport {

  static isEnable(measure) {
    return measure.stature && measure.stature > 0
  }

  static build(measure) {
    const value = measure.stature
    const birth = +measure.birthday / 1000
    const measureTime = +measure.measure_time / 1000
    const gender = measure.gender == 1 ? 0:1

    if (!value || value == 0) {
      return null;
    }
    const item = new MeasureItem()

    const heightResult = heightInfo(birth, measureTime, value, gender)

    console.log('heightResult', heightResult)
    const level = heightResult.level

    const isStand = level >= STAND_LEVEL

    item.name = "身高"

    item.levelNames = ["偏低", "正常", '偏高']
    item.value = value
    item.isStand = isStand
    item.unit = "cm"
    item.desc = heightResult.text
    item.showBar = heightResult.showBar

    // item.icon = 'report_heart_rate.png'
    if (heightResult.boundaries) {
      item.boundaries = heightResult.boundaries
      item.level = level
      item.bar = BAR

      item.min = 0
      item.max = heightResult.boundaries[1] + 40
      item.levelName = item.levelNames[item.level]
      item.levelColor = item.bar[item.level]
      // if (!isStand) {
      //   let offset = 0
      //   if (level < STAND_LEVEL) {
      //     offset = value - heightResult.boundaries[STAND_LEVEL - 1]
      //   } else {
      //     offset = value - heightResult.boundaries[STAND_LEVEL]
      //   }
      //   item.offset = offset
      // }
    }




    return item
  }

}

// 计算对应的年龄区间
function calcAgeLevel(item) {
  let l = 0
  const boundaries = [0, 3, 6, 12, 18]
  for (let i = 0; i < boundaries.length; i++) {
    // console.log('cal', day,boundaries[i])
    if (item >= boundaries[i]) {
      l++
      continue
    }
    break
  }
  return l
}

// 计算对应的身高范围

export function heightInfo(birthday, timestamp, height, gender = 0) {
  console.log("heightText", timestamp, birthday, height)
  let res = []
  // 生日大于测量时间，为异常数据
  // console.log('生日 测量日期', birthday, timestamp)
  if (+birthday > +timestamp) {
    return {
      showBar: false,
      text: ''
    }
  }

  let dateAge = getDateAge(birthday + (+new Date() / 1000 - timestamp))
  // 大于18岁
  let ageLevel = calcAgeLevel(dateAge.year)

  if (dateAge.months >= 216) {
    return {
      showBar: false,
      text: ''
    }
  }

  let targetList = childrenHeightData[gender][dateAge.year]
  let monthIndex = -1
  for (let i in targetList) {
    if (dateAge.months >= targetList[i].months) {
      monthIndex++
    } else break
  }
  console.log('targetList', targetList[monthIndex])
  targetList = targetList[monthIndex].arr

  // 设置对应区间文本
  let text
  let boundaries = [targetList[0], targetList[2]]
  let mainText = `此年龄段平均身高范围：${boundaries[0]}-${boundaries[1]}cm；`
  if (ageLevel <= 1) {
    text = [
      `宝宝身高为${height}cm，${mainText}还有很大的成长空间呢，注意保证宝宝的营养和睡眠哦~`,
      `宝宝身高为${height}cm，${mainText}宝宝生长发育良好，请继续保持哦～`,
      `宝宝身高为${height}cm，${mainText}比大多数宝宝要高哦，棒棒哒！`,
    ]
  } else if (ageLevel <= 3) {
    text = [
      `小朋友身高为${height}cm，${mainText}还有很大的成长空间呢，${ageLevel == 3 ?
        '注意保证营养和睡眠，也要多多运动哦~' : '注意保证营养和睡眠哦~'}`,
      `小朋友身高为${height}cm，${mainText}小朋友生长发育良好，请继续保持哦～`,
      `小朋友身高为${height}cm，${mainText}比大多数小朋友要高哦，${ageLevel == 3 ? '棒棒哒！': '非常棒！'}`,
    ]
  } else {
    text = [
      `您身高为${height}cm，${mainText}还有很大的成长空间呢，注意保证营养和睡眠，也要多多运动哦~`,
      `您身高为${height}cm，${mainText}生长发育良好，请继续保持哦～`,
      `您身高为${height}cm，${mainText}比大多数同龄人要高哦，非常棒！`,
    ]
  }

  // 获取身高对应的区间
  let heightLevel = 0
  if (height < boundaries[0]) heightLevel = 0
  else if (height > boundaries[1]) heightLevel = 2
  else heightLevel = 1

  console.log(`heightLevel:${heightLevel} `, height, targetList.arr)

  return {
    showBar: true,
    boundaries,
    level: heightLevel,
    text: text[heightLevel]
  }
}





// WEBPACK FOOTER //
// ./src/report/Height.js
